package cn.chiship.framework.business.biz.business.service.impl;

import cn.chiship.framework.business.biz.business.pojo.dto.BusinessFormConfigDto;
import cn.chiship.framework.business.biz.content.entity.ContentArticle;
import cn.chiship.framework.upms.biz.system.entity.UpmsCategoryDict;
import cn.chiship.framework.upms.biz.system.entity.UpmsCategoryDictExample;
import cn.chiship.framework.upms.biz.system.mapper.UpmsCategoryDictMapper;
import cn.chiship.framework.upms.biz.system.service.UpmsCategoryDictService;
import cn.chiship.sdk.cache.vo.CacheUserVO;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.id.SnowflakeIdUtil;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.business.biz.business.mapper.BusinessFormMapper;
import cn.chiship.framework.business.biz.business.entity.BusinessForm;
import cn.chiship.framework.business.biz.business.entity.BusinessFormExample;
import cn.chiship.framework.business.biz.business.service.BusinessFormService;
import cn.chiship.sdk.framework.pojo.vo.TreeVo;
import cn.chiship.sdk.framework.util.OptionUserUtil;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义表单业务接口实现层
 * 2024/12/13
 *
 * @author lijian
 */
@Service
public class BusinessFormServiceImpl extends BaseServiceImpl<BusinessForm, BusinessFormExample> implements BusinessFormService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessFormServiceImpl.class);

    @Resource
    BusinessFormMapper businessFormMapper;

    @Resource
    UpmsCategoryDictMapper upmsCategoryDictMapper;

    @Override
    public BaseResult tree() {
        List<TreeVo> treeVos = new ArrayList<>();
        UpmsCategoryDictExample upmsCategoryDictExample = new UpmsCategoryDictExample();
        upmsCategoryDictExample.createCriteria().andIsDeletedEqualTo(BaseConstants.NO).andTypeEqualTo("06");
        upmsCategoryDictExample.setOrderByClause("orders desc");
        List<UpmsCategoryDict> upmsCategoryDicts = upmsCategoryDictMapper.selectByExample(upmsCategoryDictExample);
        if (upmsCategoryDicts.isEmpty()) {
            return BaseResult.ok(treeVos);
        }
        List<String> categoryIds = new ArrayList<>();
        upmsCategoryDicts.forEach(item -> categoryIds.add(item.getId()));
        BusinessFormExample businessFormExample = new BusinessFormExample();
        businessFormExample.createCriteria()
                .andIsDeletedEqualTo(BaseConstants.NO)
                .andStatusEqualTo(BaseConstants.NO)
                .andCategoryIdIn(categoryIds);
        List<BusinessForm> businessForms = businessFormMapper.selectByExample(businessFormExample);
        for (UpmsCategoryDict category : upmsCategoryDicts) {
            TreeVo treeVo = getTreeVo(category, businessForms);
            treeVos.add(treeVo);
        }

        return BaseResult.ok(treeVos);
    }

    @NotNull
    private static TreeVo getTreeVo(UpmsCategoryDict category, List<BusinessForm> businessForms) {
        TreeVo treeVo = new TreeVo();
        treeVo.setLabel(category.getName());
        treeVo.setId(category.getId());
        treeVo.setExt("1");

        List<TreeVo> children = new ArrayList<>();
        for (BusinessForm form : businessForms) {
            TreeVo item = new TreeVo();
            item.setLabel(form.getName());
            item.setId(form.getId());
            item.setExt("2");
            item.setIcon(form.getIcon());
            if (category.getId().equals(form.getCategoryId())) {
                children.add(item);
            }
        }
        treeVo.setChildren(children);
        return treeVo;
    }

    @Override
    public BusinessForm selectByPrimaryKey(Object id) {
        BusinessFormExample businessFormExample = new BusinessFormExample();
        businessFormExample.createCriteria().andIdEqualTo(id.toString());
        List<BusinessForm> businessForms = businessFormMapper.selectByExampleWithBLOBs(businessFormExample);
        if (businessForms.isEmpty()) {
            return new BusinessForm();
        }
        return businessForms.get(0);
    }

    @Override
    public BaseResult insertSelective(BusinessForm businessForm) {
        BusinessFormExample businessFormExample = new BusinessFormExample();
        businessFormExample.createCriteria().andCodeEqualTo(businessForm.getCode());
        if (businessFormMapper.countByExample(businessFormExample) > 0) {
            return BaseResult.error("表单编号已存在，请重新输入！");
        }
        return super.insertSelective(businessForm);
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(BusinessForm businessForm) {
        BusinessFormExample businessFormExample = new BusinessFormExample();
        businessFormExample.createCriteria().andCodeEqualTo(businessForm.getCode()).andIdNotEqualTo(businessForm.getId());
        if (businessFormMapper.countByExample(businessFormExample) > 0) {
            return BaseResult.error("表单编号已存在，请重新输入！");
        }
        return super.updateByPrimaryKeySelective(businessForm);
    }

    @Override
    public BaseResult saveConfig(BusinessFormConfigDto businessFormConfigDto, Boolean isPublish) {
        BusinessForm businessForm = businessFormMapper.selectByPrimaryKey(businessFormConfigDto.getId());
        if (businessForm == null) {
            return BaseResult.error("无效的表单");
        }
        businessForm.setFormContent(businessFormConfigDto.getFormContent());
        businessForm.setFormOptions(businessFormConfigDto.getFormOption());

        if (Boolean.TRUE.equals(isPublish)) {
            businessForm.setStatus(Byte.valueOf("0"));
        }
        super.updateByPrimaryKeySelective(businessForm);
        return BaseResult.ok();
    }
}
